#!/bin/bash

[ -z "$OPENARS_HOME" ] && { echo "please, define and export OPENARS_HOME in your .progile. exit 1." >&2; exit 1; }
[ ! -z "$OPENARS_HOME" -a ! -d "$OPENARS_HOME" ] && { echo "err: no such directory OPENARS_HOME=$OPENARS_HOME" >&2; exit 1; }

cd $OPENARS_HOME
set -- $OPENARS_HOME
scr_list=`find . -name "*.sh"`
for file in $scr_list
do
    res=`cat $file | sed -ne '/^[^#]*\. \$OPENARS_HOME/p'`
    if [ ! -z "$res" ]; then
	echo "err: script $file is not tuning to work. use tools/svn2work.sh"
    fi
done
cd - >/dev/null

. `dirname $0`/../common/conf.h

main_setenv

echo -e "\tcheck main.conf:"
echo "-----------------"
if [ "$ORACLE_ENABLE" = "Y" ]; then echo "oracle enable=\"Y\""; else echo "oracle enable=\"N\""; fi
if [ "$CMDB_ENABLE" = "Y" ]; then echo "cmdb enable=\"Y\""; else echo "cmdb enable=\"N\""; fi
if [ "$ZABBIX_ENABLE" = "Y" ]; then echo "zabbix enable=\"Y\""; else echo "zabbix enable=\"N\""; fi
echo "-----------------"

SAVE=$IFS
IFS=":"
for path in $PATH
do
[ ! -d $path ] && echo "warn: PATH: no such directory $path" >&2
done
IFS=$SAVE

[ -z "$USER_ARS" ] && echo "err: not define environment USER_ARS" >&2
[ ! -z "$USER_ARS" -a -z "`grep "^$USER_ARS:" /etc/passwd`" ] && echo "err: not find user USER_ARS=$USER_ARS in /etc/passwd" >&2

if [ "$ORACLE_ENABLE" = "Y" ]; then
    [ -z "$ORACLE_HOME" ] && { echo "err: not define environment ORACLE_HOME" >&2; exit 1; }
    [ ! -z "$ORACLE_HOME" -a ! -d "$ORACLE_HOME" ] && { echo "err: no such directory ORACLE_HOME=$ORACLE_HOME" >&2; exit 1; }
    [ -z "$ORACLE_BASE" ] && echo "err: not define environment ORACLE_BASE" >&2
    [ ! -z "$ORACLE_BASE" -a ! -d "$ORACLE_BASE" ] && echo "err: no such directory ORACLE_BASE=$ORACLE_BASE"
    [ -z "$ORACLE_OWNER" ] && echo "err: not define environment ORACLE_OWNER" >&2
    [ ! -z "$ORACLE_OWNER" -a -z "`grep "^$ORACLE_OWNER:" /etc/passwd`" ] && echo "err: not find user ORACLE_OWNER=$ORACLE_OWNER in /etc/passwd" >&2
    [ -z "$ORACLE_TERM" ] && echo "err: not define environment ORACLE_TERM" >&2
    [ -z "$CLASSPATH" ] && echo "err: not define environment CLASS_PATH" >&2
    [ -z "$LIBPATH" ] && echo "err: not define environment LIB_PATH" >&2
    [ -z "$LD_LIBRARY_PATH" ] && echo "err: not define environment LD_LIBRARY_PATH" >&2
    [ -z "$LD_LIBRARY_PATH_64" ] && echo "err: not define environment LD_LIBRARY_PATH_64" >&2
    
        SAVE=$IFS
        IFS=":"
        for path in $LIBPATH
        do	
    	    [ ! -d $path ] && echo "warn: LIBPATH: no such directory $path" >&2
	done
	for path in $LD_LIBRARY_PATH
	do
	    [ ! -d $path ] && echo "warn: LD_LIBRARY_PATH: no such directory $path" >&2
	done
	for path in $LD_LIBRARY_PATH_64
	do
	    [ ! -d $path ] && echo "warn: LD_LIBRARY_PATH_64: no such directory $path" >&2
	done
	IFS=$SAVE
    [ -z "$CONNECT_ARS" ] && echo "err: not define environment CONNECT_ARS" >&2
    conn=`sqlplus -s /NOLOG << END
	WHENEVER SQLERROR EXIT SQL.SQLCODE						       
	connect $CONNECT_ARS
	exit;
    END`
    [ "$?" -ne 0 ] && echo "err: NOT connect to CONNECT_ARS=$CONNECT_ARS" >&2
fi


if [ "$CMDB_ENABLE" = "Y" ]; then
    [ -z "$CONNECT_CMDB" ] && echo "err: not define environment CONNECT_CMDB" >&2
    conn=`sqlplus -s /NOLOG << END
	WHENEVER SQLERROR EXIT SQL.SQLCODE						       
	connect $CONNECT_CMDB
	exit;
    END`
    [ "$?" -ne 0 ] && echo "err: NOT connect to CONNECT_CMDB=$CONNECT_CMDB" >&2
fi

if [ "$ZABBIX_ENABLE" = "Y" ]; then
    [ -z "$USER_ZABBIX" ] && echo "err: not define environment USER_ZABBIX" >&2
    [ ! -z "$USER_ZABBIX" -a -z "`grep "^$USER_ZABBIX:" /etc/passwd`" ] && echo "err: not find user USER_ZABBIX=$USER_ZABBIX in /etc/passwd" >&2

    [ -z "$CONNECT_ZABBIX" ] && echo "err: not define environment CONNECT_ZABBIX" >&2
    conn=`sqlplus -s /NOLOG << END
	WHENEVER SQLERROR EXIT SQL.SQLCODE						       
	connect $CONNECT_ZABBIX
	exit;
    END`
    [ "$?" -ne 0 ] && echo "err: NOT connect to CONNECT_ZABBIX=$CONNECT_ZABBIX" >&2

    [ -z "$REPOSITORY_PATH" ] && echo "err: not define environment REPOSITORY_PATH" >&2
    [ ! -z "$REPOSITORY_PATH" -a ! -d "$REPOSITORY_PATH" ] && "err: no such directory REPOSITORY_PATH=$REPOSITORY_PATH"
fi
